package com.project.les;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import com.project.les.view.elements.FeriadoViewElement;

public class DataHelper {

	private static final String DATABASE_NAME = "feriados.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "feriados";

	// private Context context;
	private SQLiteDatabase db;
	private static DataHelper __instance;

	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into "
			+ TABLE_NAME
			+ "(name, description, day, month, year, isNacional, isState, state, city) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";

	private DataHelper() {
	}

	public static DataHelper getInstance() {
		if (DataHelper.__instance == null)
			DataHelper.__instance = new DataHelper();

		return DataHelper.__instance;
	}

	public void resetDatabase(Context context) {
		context.deleteDatabase(DATABASE_NAME);
		OpenHelper openHelper = new OpenHelper(context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
	}

	public Boolean isPrepared() {
		return this.db != null;
	}

	public long insert(String name, String description, int day, int month,
			int year, int isNacional, int isState, String state, String city) {
		this.insertStmt.bindString(1, name);
		this.insertStmt.bindString(2, description);
		this.insertStmt.bindDouble(3, day);
		this.insertStmt.bindDouble(4, month);
		this.insertStmt.bindDouble(5, year);
		this.insertStmt.bindDouble(6, isNacional);
		this.insertStmt.bindDouble(7, isState);
		this.insertStmt.bindString(8, state);
		this.insertStmt.bindString(9, city);

		return this.insertStmt.executeInsert();
	}

	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}

	public List<FeriadoViewElement> selectAll(String selection) {
		return this.selectAll(selection, "year, month, day, name asc");
	}

	public List<FeriadoViewElement> selectAll(String selection, String orderBy) {
		List<FeriadoViewElement> list = new ArrayList<FeriadoViewElement>();

		Cursor cursor = this.db
				.query(TABLE_NAME, new String[] { "id", "name", "state",
						"city", "description", "day", "month", "year",
						"isNacional", "isState" }, selection, null, null, null,
						orderBy);

		if (cursor.moveToFirst()) {
			do {

				list.add(new FeriadoViewElement(cursor.getInt(0), cursor
						.getInt(5), cursor.getInt(6), cursor.getInt(7), cursor
						.getString(1), cursor.getString(2),
						cursor.getString(3), cursor.getString(4), cursor
								.getInt(8), cursor.getInt(9)));

			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		List<FeriadoViewElement> feriadosInOrder = orderCurrentDate(list);
		return feriadosInOrder;
	}

	private List<FeriadoViewElement> orderCurrentDate(
			List<FeriadoViewElement> data) {
		GregorianCalendar currentCalendar = new GregorianCalendar();
		List<FeriadoViewElement> feriadosInOrder = new ArrayList<FeriadoViewElement>();
		int indice = 0;
		for (int i = 0; i < data.size(); i++)
			if (data.get(i).getDate().get(Calendar.DAY_OF_YEAR) >= currentCalendar
					.get(Calendar.DAY_OF_YEAR))
				indice = i;
		for (int i = indice; i < data.size(); i++) {
			feriadosInOrder.add(data.get(i));
		}
		for (int i = 0; i < indice; i++) {
			feriadosInOrder.add(data.get(i));
		}
		return feriadosInOrder;
	}

	private static class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + TABLE_NAME + "("
					+ "id INTEGER PRIMARY KEY, " + "name TEXT, "
					+ "description TEXT, " + "day INTEGER, "
					+ "month INTEGER, " + "year INTEGER, "
					+ "isNacional INTEGER, " + "isState INTEGER, "
					+ "state INTEGER, " + "city INTEGER" + ")");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("Example",
					"Upgrading database, this will drop tables and recreate.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
}